/**
 * 公司company.js
 * 版本号： v1.0.0
 */
$(function () {
    //加载公司类型列表
    $.ajax({
        url: CONTEXT_PATH + "/commonDict/getCommonDictOptions.do?modelCode=PR&funCode=unitProperty",
        type: "get",
        success: function (data) {
            var cpTypeCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                cpTypeCombo.push({"text": data[i].propertyName, "value": data[i].cdId});
            }
            $("#compTypeTemp").combobox("loadData", cpTypeCombo);
            $("#companyType").combobox("loadData", cpTypeCombo);
        }
    });
    var lastChecked = null;
    $("#company_dataList").treegrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        border: false,
        striped: true,
        pagination: true,
        pageSize: 10,
        pageList: [10, 50, 100, 150, 200],
        url: CONTEXT_PATH + '/company/companyList.do',
        idField: 'companyId',
        treeField: 'name',
        frozenColumns: [[
            {field: 'companyId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'name', title: '公司名称', width: 280, fixed: true},
                {field: 'master', title: '公司法人', width: 70, fixed: true},
                {field: 'registerID', title: '营业执照号', width: 180, fixed: true},
                {field: 'linkmanName', title: '联系人', width: 70, fixed: true},
                {field: 'linkmanPhone', title: '联系人手机号', width: 90, fixed: true},
                {field: 'registerDateFormat', title: '注册日期', width: 150}
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#company_dataList").treegrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#company_dataList').treegrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (row, data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#company_dataList").treegrid("select", data.rows[0].companyId);
            }
        }
    });

    //定义可操作按钮
    $("#company_Menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            company_findList();
                        }
                    });
                }
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            company_add();
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            var checked = $("#company_dataList").treegrid("getChecked");
                            if (!checked || checked.length == 0) {
                                $.messager.show({
                                    title: '操作提示',
                                    msg: "请选择一条数据后再进行修改操作",
                                    timeout: 3000,
                                    showType: "slide"
                                });
                                return;
                            };
                            var row = $("#company_dataList").treegrid("getSelected");
                            var compId = row.companyId;
                            window.location.href = CONTEXT_PATH + "/company/toCompany.do?compId=" + compId;
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            company_del();
                        }
                    });
                }
                //查看详细按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            company_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加公司方法
 */
function company_add() {
    window.location.href = CONTEXT_PATH + "/company/toCompany.do";
}

/**
 * 查看公司详情
 */
function company_view(iconCls) {
    var checked = $("#company_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择要查看的公司", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#company_dataList").treegrid("getSelected");
    //IFRAM打开窗口
    layoutSub(gsxq.id + "company", gsxq.url+ row.companyId, iconCls, gsxq.closable, gsxq.title);
}


/**
 * 删除公司信息
 */
function company_del() {
    var checked = $("#company_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择要删除的公司", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#company_dataList").treegrid("getSelected");
    var node = $('#company_dataList').treegrid('getChildren', row.companyId);
    if (node.length > 0) {
        $.messager.show({title: '操作提示', msg: "父公司不能删除，请重新选择", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    $.messager.confirm('删除提示', '您确定要删除 ' + row.name + '吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/company/deleteCompany.do?id=" + row.companyId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#company_dataList").treegrid("reload", {
                        name: $("#companyName").textbox('getValue'),
                        companyType: $("#companyType").combobox('getValue'),
                        registerID: $("#registerID").textbox('getValue')
                    });
                }
            })
        }
    });
}

/**
 * 查询公司信息
 */
function company_findList() {
    refreshTreePageNumber("company_dataList");
    $("#company_dataList").treegrid("reload", {
        name: $("#companyName").textbox('getValue'),
        companyType: $("#companyType").combobox('getValue'),
        registerID: $("#registerID").textbox('getValue')
    });
    $("#company_dataList").treegrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshTreePageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).treegrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).treegrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}





